package com.solcoo.merchant.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.android.common.App;
import com.android.common.asyntask.AsyncTask;
import com.android.common.util.DialogUtil;
import com.android.common.util.StringUtil;
import com.solcoo.merchant.ApplicationExt;
import com.solcoo.merchant.R;
import com.solcoo.merchant.base.i.BaseViewWrapper;
import com.solcoo.merchant.base.task.BindContextAsyncTask;
import com.solcoo.merchant.ui.login.LoginActivity;
import com.solcoo.merchant.util.ViewUtil;

public class BaseActivity extends Activity implements BaseViewWrapper {

	protected static final String TAG = "Activity";

	protected ProgressDialog progressDialog;
	private View mLinearNoData;

	private String title;

	private static final int LOAD_VIEW_TASK_TAG_KEY = "loadViewTask".hashCode();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.v(TAG, "onCreate activity:" + toString());
		progressDialog = DialogUtil.createProgressDialog(this,
				R.string.do_loading);
		App.onCreateActivity(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!(this instanceof LoginActivity)) {
			ViewUtil.setGoBackClickListener(findViewById(R.id.btn_go_back));
		}

		View titleView = findViewById(R.id.title);
		if (StringUtil.isBlank(title) && titleView != null) {
			((TextView) titleView).setText(App.getActivityLabel(this));
		}
		ApplicationExt.getInstance().setCurrentContext(this);
	}

	@Override
	protected void onDestroy() {
		BindContextAsyncTask.clearTask(this);
		App.onDestoryActivity(this);
		super.onDestroy();
	}

	@Override
	public void dismissNotData() {
		if (mLinearNoData != null) {
			mLinearNoData.setVisibility(View.GONE);
		}
	}

	protected View inflateNoDataView() {
		if (mLinearNoData == null) {
			mLinearNoData = LayoutInflater.from(this).inflate(R.layout.no_data,
					null);
		}
		return mLinearNoData;
	}

	public void dismissProgressDialog() {
		DialogUtil.dimissDialog(progressDialog);
	}

	public void setProgressDialogMessage(String msg) {
		progressDialog.setMessage(msg);
	}

	@Override
	public void showProgressDialog() {
		if (progressDialog != null)
			progressDialog.show();
	}

	public void setTitle(String title) {
		this.title = title;
		TextView tv = (TextView) findViewById(R.id.title);
		tv.setText(title);
	}

	@Override
	public View showNotData() {
		if (mLinearNoData == null) {
			mLinearNoData = inflateNoDataView();
		}
		ViewUtil.showNotData(mLinearNoData, this);
		return mLinearNoData;
	}

	@Override
	public void onNetWorkError(AsyncTask<?, ?, ?> task) {
		showNotData();
		if (mLinearNoData == null) {
			return;
		}
		TextView wordView = (TextView) mLinearNoData
				.findViewById(R.id.no_data_desc);
		wordView.setText("网络异常");
		wordView.setVisibility(View.VISIBLE);
		View btn = mLinearNoData.findViewById(R.id.retry);
		btn.setVisibility(View.VISIBLE);
		btn.setTag(LOAD_VIEW_TASK_TAG_KEY, task);
	}

	public void retryOnClick(View v) {
		mLinearNoData.setVisibility(View.GONE);
		((AsyncTask<?, ?, ?>) v.getTag(LOAD_VIEW_TASK_TAG_KEY)).reStart();
	}

}
